package com.dean.utils;

import com.dean.entity.DeanDbInfo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author dean
 * @version 1.0
 * @description: 数据库url工具类
 * @date 2023/7/18 11:26
 */
public class DeanDbUrlUtils {
    private final  static  String REGEX ="jdbc:(?<db>\\w+):.*((//)|@)(?<host>.+):(?<port>\\d+)(/|(;DatabaseName=)|:)(?<dbName>\\w+-?\\w+)\\??.*";
    public static DeanDbInfo getHostFromUrl(String url){
        DeanDbInfo  deanDbInfo = new DeanDbInfo();
        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(url);
        if(m.find()) {
            deanDbInfo.setDbType(m.group("db"));
            deanDbInfo.setHost(m.group("host"));
            deanDbInfo.setPort(m.group("port"));
            deanDbInfo.setDbName(m.group("dbName"));
        }
        return  deanDbInfo;
    }
}
